package com.linkoog.devtools.code_generator.wizard.generate.database.table.dialog;

import com.linkoog.devtools.code_generator.database.domain.ConnectionInfo;
import com.linkoog.devtools.code_generator.setting.persistent.CodeGenDevToolsPersistent;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Locale;

/**
 * @author xiaoyu
 */
public class ConnectionListDialog extends DialogWrapper {


    private JPanel myMainPanel;
    private JLabel myConnLabel;
    private JTable connectionTable;
    private JButton addConnButton;
    private JButton editConnButton;
    private JButton removeConnButton;


    public ConnectionListDialog(JComponent jComponent) {
        super(jComponent, true);
        $$$setupUI$$$();
        setTitle("配置数据源");
        myConnLabel.setText("选择连接:");
        myMainPanel.setMinimumSize(new Dimension(400, 380));


        addConnButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ConnectionOperationDialog dialog = new ConnectionOperationDialog(myMainPanel, connectionInfo -> {
                    System.out.println(connectionInfo);
                    List<ConnectionInfo> conns = CodeGenDevToolsPersistent.getInstance().getConnectionInfoList();
                    conns.add(connectionInfo);

                    //myConnsList.removeAll();
                    for (ConnectionInfo conn : conns) {
                        //myConnsList.setListData();
                    }
                    //crudList.setSelectedIndex(0);
                });
                dialog.show();
            }
        });

        editConnButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = connectionTable.getSelectedRow();
                if (index == -1) {
                    Messages.showErrorDialog(myMainPanel, "请选择要修改的连接");
                    return;
                }
                ConnectionInfo conn = CodeGenDevToolsPersistent.getInstance().getConnectionInfoList().get(index);
                /*CrudEditConnDialog dialog = new CrudEditConnDialog(ConnectionListDialog.this, conn);
                if (!dialog.showAndGet()) {
                    return;
                }*/
            }
        });

        removeConnButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = connectionTable.getSelectedRow();
                if (index == -1) {
                    Messages.showErrorDialog(connectionTable, "请选择要删除的连接");
                    return;
                }

                int result = Messages.showYesNoDialog(connectionTable.getSelectedRow() + "", "确认删除？", Messages.getQuestionIcon());
                if (result == Messages.YES) {
                    CodeGenDevToolsPersistent.getInstance().getConnectionInfoList().remove(index);

                    /*CrudList crudList = getCrudList();
                    crudList.clearElement();
                    List<Conn> conns = CrudSettings.getInstance().getConns();
                    for (Conn conn : conns) {
                        crudList.addElement(new ListElement(CrudIcons.MYSQL_CONN, conn.getName()));
                    }*/
                }
            }
        });

        init();
    }

    @Override
    protected @Nullable ValidationInfo doValidate() {
        return super.doValidate();
    }

    @Override
    public void doCancelAction() {
        super.doCancelAction();
    }

    @Override
    protected void doOKAction() {
        super.doOKAction();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return myMainPanel;
    }


    private void createUIComponents() {

    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        myMainPanel.add(panel1, new GridConstraints(1, 0, 2, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        panel1.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        scrollPane1.setViewportView(connectionTable);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(4, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        addConnButton = new JButton();
        addConnButton.setEnabled(true);
        addConnButton.setText("添加连接");
        panel2.add(addConnButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel2.add(spacer1, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        editConnButton = new JButton();
        editConnButton.setText("编辑连接");
        panel2.add(editConnButton, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        removeConnButton = new JButton();
        removeConnButton.setText("删除连接");
        panel2.add(removeConnButton, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        myConnLabel = new JLabel();
        Font myConnLabelFont = this.$$$getFont$$$(null, -1, 16, myConnLabel.getFont());
        if (myConnLabelFont != null) myConnLabel.setFont(myConnLabelFont);
        myConnLabel.setText("");
        myMainPanel.add(myConnLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
